/**
 * 
 */
package com.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.model.daemon.MetaDelete;

/**
 * 
 * The model layer relevant to item module.
 * 
 * @author cyfex
 *
 */
public class ItemService {
	/*
	 * Notice that all parameter validation should be done in Servlet who
	 * control the visit to this model.
	 * 
	 * Transform item relevant database content to Excel file. If the process
	 * succeed, generate the excel download link. Otherwise, return null value.
	 * Before returning, itemExport() should generate a daemon thread to clean
	 * the meta files.
	 */
	public static Boolean itemEdit(HttpServletRequest request) {
		Long iid = Long.parseLong( request.getParameter("iid") );
		String name = request.getParameter("name");
		String brand = request.getParameter("brand");
		double amount = Double.parseDouble( request.getParameter("amount") );
		String unit = request.getParameter("unit");
		String store = request.getParameter("store");
		String description = request.getParameter("description");
		String person = request.getParameter("person");
		
		//Connect DB
		try {
			Connection con = DataBase.getConnection();
			PreparedStatement pstmtEdit = con.prepareStatement("UPDATE item SET name=?,brand=?,amount=?,unit=?,store=?,description=?,person=? WHERE iid=?");
			pstmtEdit.setString(1, name);
			pstmtEdit.setString(2, brand);
			pstmtEdit.setDouble(3, amount);
			pstmtEdit.setString(4, unit);
			pstmtEdit.setString(5, store);
			pstmtEdit.setString(6, description);
			pstmtEdit.setString(7, person);
			pstmtEdit.setLong(8, iid);

			pstmtEdit.execute();
			
			//close work
			con.close();
			return true;
		} catch (SQLException e2) {
			e2.printStackTrace();
			return false;
		}
	}
	
	public static String itemExport(HttpServletRequest request) {
		// Connect Database
		try {
			Connection con = DataBase.getConnection();

			/*
			 * Create the excel file, obtaining data from DB
			 */
			String excelPath = request.getSession().getServletContext()
					.getRealPath("downloads");
			File excelDir = new File(excelPath);
			if (!excelDir.exists()) {
				excelDir.mkdir();
			}
			Workbook wb = new HSSFWorkbook();
			Sheet sheetItem = wb.createSheet("物品信息");
		    Sheet sheetHistory = wb.createSheet("出入库信息");
		    
		    /*
		     * sheetItem part
		     */
		    Row rowItemTitle = sheetItem.createRow((short)0);
		    rowItemTitle.createCell(0).setCellValue("物品名称");
		    rowItemTitle.createCell(1).setCellValue("品牌");
		    rowItemTitle.createCell(2).setCellValue("数量");
		    rowItemTitle.createCell(3).setCellValue("单位");
		    rowItemTitle.createCell(4).setCellValue("库房");
		    rowItemTitle.createCell(5).setCellValue("备注");
		    rowItemTitle.createCell(6).setCellValue("管理者");
		    PreparedStatement psItem=con.prepareStatement("SELECT name,brand,amount,unit,store,description,person FROM item");
		    ResultSet rsItem=psItem.executeQuery();
		    PreparedStatement psStore=con.prepareStatement("SELECT room FROM store_room WHERE name=?");
		    ResultSet rsStore=null;
		    String itemBrand,itemUnit,itemDescription,itemPerson;
		    String store=null;
		    int i=1;
		    while(rsItem.next()){
		    	// add a new line
		    	Row rowItem = sheetItem.createRow(i);
		    	i++;
		    	// add content of item information
		    	rowItem.createCell(0).setCellValue(rsItem.getString(1));
		    	if( (itemBrand=rsItem.getString(2))!=null )
		    		rowItem.createCell(1).setCellValue(itemBrand);
		    	rowItem.createCell(2).setCellValue(rsItem.getString(3));
		    	if( (itemUnit=rsItem.getString(4))!=null )
		    		rowItem.createCell(3).setCellValue(itemUnit);
		    	store = rsItem.getString(5);
		    	psStore.setString(1, store);
		    	rsStore = psStore.executeQuery();
		    	if(rsStore.next())
		    		rowItem.createCell(4).setCellValue(rsItem.getString(5)+"("+rsStore.getString(1)+")");
		    	if( (itemDescription=rsItem.getString(6))!=null )
		    		rowItem.createCell(5).setCellValue(itemDescription);
		    	if( (itemPerson=rsItem.getString(7))!=null )
		    		rowItem.createCell(6).setCellValue(itemPerson);
		    }
		    rsItem.close();
		    
		    /*
		     * sheetHistory part
		     */
		    Row rowHistoryTitle = sheetHistory.createRow((short)0);
		    rowHistoryTitle.createCell(0).setCellValue("品牌");
		    rowHistoryTitle.createCell(1).setCellValue("物品名称");
		    rowHistoryTitle.createCell(2).setCellValue("单位");
		    rowHistoryTitle.createCell(3).setCellValue("数量");
		    rowHistoryTitle.createCell(4).setCellValue("事件");
		    rowHistoryTitle.createCell(5).setCellValue("从");
		    rowHistoryTitle.createCell(6).setCellValue("到");
		    rowHistoryTitle.createCell(7).setCellValue("时间");
		    rowHistoryTitle.createCell(8).setCellValue("经手人");
		    rowHistoryTitle.createCell(9).setCellValue("备注");
		    PreparedStatement psHistory=con.prepareStatement("SELECT brand,name,unit,number,`from`,`to`,time,person,remark FROM item_history LEFT JOIN (SELECT brand,name,unit,iid FROM item) AS i ON i.iid=item_history.iid");
		    ResultSet rsHistory=psHistory.executeQuery();
		    String hsFrom,hsTo,hsRemark;
		    int j=1;
		    while(rsHistory.next()){
		    	// add a new line
		    	Row rowHistory = sheetHistory.createRow(j);
		    	j++;
		    	// add content of history infomation
		    	if( (itemBrand=rsHistory.getString(1))!=null )
		    		rowHistory.createCell(0).setCellValue(itemBrand);
		    	rowHistory.createCell(1).setCellValue(rsHistory.getString(2));
		    	if( (itemUnit=rsHistory.getString(3))!=null )
		    		rowHistory.createCell(2).setCellValue(itemUnit);
		    	rowHistory.createCell(3).setCellValue(rsHistory.getString(4));
		    	if( (hsFrom=rsHistory.getString(5))!=null )
		    		rowHistory.createCell(5).setCellValue(hsFrom);
		    	if( (hsTo=rsHistory.getString(6))!=null )
		    		rowHistory.createCell(6).setCellValue(hsTo);
		    	rowHistory.createCell(7).setCellValue(rsHistory.getString(7));
		    	if( (itemPerson=rsHistory.getString(8))!=null )
		    		rowHistory.createCell(8).setCellValue(itemPerson);
		    	if( (hsRemark=rsHistory.getString(9))!=null )
		    		rowHistory.createCell(9).setCellValue(hsRemark);
		    	// content of "事件"
		    	if( hsFrom==null )
		    		rowHistory.createCell(4).setCellValue("入库");
		    	else if( hsTo==null )
		    		rowHistory.createCell(4).setCellValue("出库");
		    	else
		    		rowHistory.createCell(4).setCellValue("转移");
		    }
		    rsHistory.close();
		    
		    // Absolute path
			excelPath = request
					.getSession()
					.getServletContext()
					.getRealPath(
							"downloads" + File.separatorChar + "item-export-"
									+ System.currentTimeMillis()) + ".xls";
			FileOutputStream fileOut = new FileOutputStream(excelPath);
			wb.write(fileOut);
			fileOut.close();

			// Start the singleton daemon thread.
			MetaDelete.getInSingleton(request);
			
			// Context path
			excelPath = excelPath.substring(request.getSession().getServletContext().getRealPath("/").length());
			excelPath = excelPath.replaceAll("\\\\", "/");
			
			// Return the context path
			return excelPath;
		} catch (Exception e) {
			e.printStackTrace();
			MetaDelete.getInSingleton(request);
			return null;
		}
	}
	
	public static Boolean itemImport(HttpServletRequest request) throws ServletException, IOException {
		/*
		 * UploadFile
		 */
		// Check that we have a file upload request
		//boolean isMultipart = ServletFileUpload.isMultipartContent(request);
		// Create a new file upload handler
		ServletFileUpload upload = new ServletFileUpload();
		// Parse the request
		FileItemIterator iter;
		String uploadedPath = null;
		try {
			iter = upload.getItemIterator(request);
			while (iter.hasNext()) {
			    FileItemStream item = iter.next();
			    InputStream stream = item.openStream();
			    if (!item.isFormField()) {
			        // Process the input stream
			        uploadedPath = request.getSession().getServletContext().getRealPath("uploaded");
			        File uploadedExcel = new File( uploadedPath );
			        if( !uploadedExcel.exists() ) {
			        	uploadedExcel.mkdir();
			        }
			        // Upload file
			        uploadedPath = request.getSession().getServletContext().getRealPath("uploaded"+File.separatorChar+System.currentTimeMillis());
			        OutputStream os = new FileOutputStream(uploadedPath);
			        byte buf[] = new byte[1024];  
                    int length = 0;    
                    while( (length = stream.read(buf)) > 0 ){    
                        os.write(buf, 0, length);    
                    }
                    // Close work
                    os.flush();  
                    os.close();
                    stream.close();
			    }
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
			/*
			 * Delete uploaded file
			 */
			File uploadedExcel = new File( uploadedPath );
			if( uploadedExcel.exists() && !uploadedExcel.equals(request.getSession().getServletContext().getRealPath("uploaded")) )
				uploadedExcel.delete();
			return false;
		}
		
		/*
		 * Insert from excel into database
		 */
		//Connect DB
		try {
			Connection con = DataBase.getConnection();
			PreparedStatement pstmtCheck = con.prepareStatement("SELECT * FROM item WHERE name=? AND brand=? AND unit=?");
			ResultSet rsCheck = null;
			PreparedStatement pstmtImport = con.prepareStatement("INSERT INTO item VALUES(null,?,?,?,?,?,?,?)");
			
			HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(uploadedPath));
			for( int k=0; k<wb.getNumberOfSheets(); k++ ) {
				HSSFSheet sheet = wb.getSheetAt(k);
				int rows = sheet.getPhysicalNumberOfRows();
				for (int r=0; r < rows; r++) {
					HSSFRow row = sheet.getRow(r);
					if (row == null) {
						continue;
					}

					HSSFCell cellName = row.getCell(0);
					HSSFCell cellBrand = row.getCell(1);
					HSSFCell cellAmount = row.getCell(2);
					HSSFCell cellUnit= row.getCell(3);
					HSSFCell cellStore = row.getCell(4);
					HSSFCell cellDspt = row.getCell(5);
					HSSFCell cellPerson = row.getCell(6);
					
					if( cellName==null || cellBrand==null || cellAmount==null || cellUnit==null || cellStore==null || cellDspt==null || cellPerson==null ) {
						continue;
					}
					
					String name = cellName.getStringCellValue();
					String brand = cellBrand.getStringCellValue();
					Double amount = cellAmount.getNumericCellValue();
					String unit = cellUnit.getStringCellValue();
					String store = cellStore.getStringCellValue();
					String dspt = cellDspt.getStringCellValue();
					String person = cellPerson.getStringCellValue();
					
					pstmtCheck.setString(1, name);
					pstmtCheck.setString(2, brand);
					pstmtCheck.setString(3, unit);
					rsCheck = pstmtCheck.executeQuery();
					if( rsCheck.next() ) {
						// do NOTHING.
						;
					}
					else if( name==null || brand==null || amount==null || unit==null || store==null || dspt==null || person==null) {
						;
					}
					else {
						pstmtImport.setString(1, name);
						pstmtImport.setString(2, brand);
						pstmtImport.setDouble(3, amount);
						pstmtImport.setString(4, unit);
						pstmtImport.setString(5, store);
						pstmtImport.setString(6, dspt);
						pstmtImport.setString(7, person);
						pstmtImport.execute();
					} 
					rsCheck.close();
				}
			}				
			
			//close work
			con.close();
		} catch (SQLException e2) {
			e2.printStackTrace();
			/*
			 * Delete uploaded file
			 */
			File uploadedExcel = new File( uploadedPath );
			if( uploadedExcel.exists() && !uploadedExcel.equals(request.getSession().getServletContext().getRealPath("uploaded")) )
				uploadedExcel.delete();
			return false;
		}
		
		/*
		 * Delete uploaded file
		 */
		File uploadedExcel = new File( uploadedPath );
		if( uploadedExcel.exists() && !uploadedExcel.equals(request.getSession().getServletContext().getRealPath("uploaded")) )
			uploadedExcel.delete();
		
		/*
		 * Page redirection
		 */
		return true;
	}
	
	public static Boolean itemIn(HttpServletRequest request) {
		Long iid = Long.parseLong( request.getParameter("iid") );
		String name = request.getParameter("name");
		String brand = request.getParameter("brand");
		String unit = request.getParameter("unit");
		double amount = Math.abs(Double.parseDouble( request.getParameter("amount") ));
		String store = request.getParameter("store");
		String room = request.getParameter("room");
		String description = request.getParameter("description");
		String person = request.getParameter("person");		
		
		//Connect DB
		try {
			Connection con = DataBase.getConnection();

			PreparedStatement pstmtStrExist = con.prepareStatement("SELECT * FROM store_room WHERE name=?");
			pstmtStrExist.setString(1, store);
			ResultSet rsStrEx = pstmtStrExist.executeQuery();
			if( rsStrEx.next() )
				;
			else {
				PreparedStatement pstmtNewStr = con.prepareStatement("INSERT INTO store_room VALUES(?,?)");
				pstmtNewStr.setString(1, store);
				pstmtNewStr.setString(2, room);
				pstmtNewStr.execute();
			}
			rsStrEx.close();

			PreparedStatement pstmtCheckIID = con.prepareStatement("SELECT * FROM item WHERE iid=?");
			PreparedStatement pstmtCheckStore = con.prepareStatement("SELECT * FROM item WHERE name=? AND brand=? AND unit=? AND store=?");
			
			String nameTemp = name;
			String brandTemp = brand;
			String unitTemp = unit;
			String dscpTemp = description;	// Description
			
			pstmtCheckIID.setLong(1, iid);
			ResultSet rsIID = pstmtCheckIID.executeQuery();
			if( !rsIID.next() ) {
				if( iid==0 ) {
					// newly added Imported Item.
					/*pstmtCheckStore.setString(1, name);
					pstmtCheckStore.setString(2, brand);
					pstmtCheckStore.setString(3, unit);
					pstmtCheckStore.setString(4, store);
					ResultSet rsStore = pstmtCheckStore.executeQuery();*/
					;
				}
				else {
					rsIID.close();
					con.close();
					return false;
				}
			}
			else {
				nameTemp = rsIID.getString("name");
				brandTemp = rsIID.getString("brand");
				unitTemp = rsIID.getString("unit");
				dscpTemp = rsIID.getString("description");
			}
			pstmtCheckStore.setString(1, nameTemp);
			pstmtCheckStore.setString(2, brandTemp);
			pstmtCheckStore.setString(3, unitTemp);
			pstmtCheckStore.setString(4, store);
			ResultSet rsStore = pstmtCheckStore.executeQuery();
			if( rsStore.next() ) {
				// indicate that the corresponding store location has already exist in table "item", plan 1.
				PreparedStatement pstmt1_1 = con.prepareStatement("UPDATE item SET amount=amount+? WHERE name=? AND brand=? AND unit=? AND store=?");
				PreparedStatement pstmt1_2 = con.prepareStatement("INSERT INTO item_history VALUES(null,?,?,?,?,?,?,?)");
				
				pstmt1_1.setDouble(1, amount);
				pstmt1_1.setString(2, nameTemp);
				pstmt1_1.setString(3, brandTemp);
				pstmt1_1.setString(4, unitTemp);
				pstmt1_1.setString(5, store);
				
				pstmt1_2.setLong(1, rsStore.getLong("iid"));
				pstmt1_2.setDouble(2, amount);
				pstmt1_2.setString(3, null);
				pstmt1_2.setString(4, store);
				pstmt1_2.setTimestamp(5, new Timestamp(new java.util.Date().getTime()));
				pstmt1_2.setString(6, person);
				pstmt1_2.setString(7, description);
				
				pstmt1_1.execute();
				pstmt1_2.execute();
			}
			else {
				// indicate that the corresponding store location does not exist in table "item", plan 2.
				PreparedStatement pstmt2_1 = con.prepareStatement("INSERT INTO item VALUES(null,?,?,?,?,?,?,?)",PreparedStatement.RETURN_GENERATED_KEYS);
				PreparedStatement pstmt2_2 = con.prepareStatement("INSERT INTO item_history VALUES(null,?,?,?,?,?,?,?)");
				pstmt2_1.setString(1, nameTemp);
				pstmt2_1.setString(2, brandTemp);
				pstmt2_1.setDouble(3, amount);
				pstmt2_1.setString(4, unitTemp);
				pstmt2_1.setString(5, store);
				pstmt2_1.setString(6, dscpTemp);
				pstmt2_1.setString(7, person);
				
				long iidTemp = 0;
				pstmt2_1.executeUpdate();
				ResultSet rsIK = pstmt2_1.getGeneratedKeys();	// Identity key
				if( rsIK.next() )
					iidTemp = rsIK.getLong(1);
				else
					iidTemp = Long.MAX_VALUE;
				rsIK.close();
				
				pstmt2_2.setLong(1, iidTemp);
				pstmt2_2.setDouble(2, amount);
				pstmt2_2.setString(3, null);
				pstmt2_2.setString(4, store);
				pstmt2_2.setTimestamp(5, new Timestamp(new java.util.Date().getTime()));
				pstmt2_2.setString(6, person);
				pstmt2_2.setString(7, description);
				
				pstmt2_2.execute();
			}
			rsStore.close();
			rsIID.close();
			//close work
			con.close();
			return true;
		} catch (SQLException e2) {
			e2.printStackTrace();
			return false;
		}
	}
	
	public static Boolean itemOut(HttpServletRequest request) {
		Long iid = Long.parseLong( request.getParameter("iid") );
		double amount = Math.abs(Double.parseDouble( request.getParameter("amount") ));
		String slt = request.getParameter("slt");
		String store = request.getParameter("store");
		String room = request.getParameter("room");
		String description = request.getParameter("description");
		String person = request.getParameter("person");		
		
		//Connect DB
		try {
			Connection con = DataBase.getConnection();

			PreparedStatement pstmtStrExist = con.prepareStatement("SELECT * FROM store_room WHERE name=?");
			pstmtStrExist.setString(1, store);
			ResultSet rsStrEx = pstmtStrExist.executeQuery();
			if( rsStrEx.next() )
				;
			else if(!slt.equals("直接出库")){
				PreparedStatement pstmtNewStr = con.prepareStatement("INSERT INTO store_room VALUES(?,?)");
				pstmtNewStr.setString(1, store);
				pstmtNewStr.setString(2, room);
				pstmtNewStr.execute();
			}
			rsStrEx.close();

			PreparedStatement pstmtCheckIID = con.prepareStatement("SELECT * FROM item WHERE iid=?");
			PreparedStatement pstmtCheckItem = con.prepareStatement("SELECT * FROM item WHERE name=? AND brand=? AND unit=? AND store=?");

			pstmtCheckIID.setLong(1, iid);
			ResultSet rsIID = pstmtCheckIID.executeQuery();
			if( !rsIID.next() ) {
				return false;
			}
			else {
				if( rsIID.getDouble("amount")<amount ){
					return false;
				}
				else if( slt.equals("直接出库") ) {
					PreparedStatement pstmt0_1 = con.prepareStatement("UPDATE item SET amount=amount-? WHERE iid=?");
					PreparedStatement pstmt0_2 = con.prepareStatement("INSERT INTO item_history VALUES(null,?,?,?,?,?,?,?)");
					
					pstmt0_1.setDouble(1, amount);
					pstmt0_1.setLong(2, iid);	
					
					pstmt0_2.setLong(1, iid);
					pstmt0_2.setDouble(2, -amount);
					pstmt0_2.setString(3, rsIID.getString("store"));
					pstmt0_2.setString(4, null);
					pstmt0_2.setTimestamp(5, new Timestamp(new java.util.Date().getTime()));
					pstmt0_2.setString(6, person);
					pstmt0_2.setString(7, description);
					
					pstmt0_1.execute();
					pstmt0_2.execute();
				}
				else {
					pstmtCheckItem.setString(1, rsIID.getString("name"));
					pstmtCheckItem.setString(2, rsIID.getString("brand"));
					pstmtCheckItem.setString(3, rsIID.getString("unit"));
					pstmtCheckItem.setString(4, store);
					ResultSet rsItem = pstmtCheckItem.executeQuery();
					if( rsItem.next() ) {
						// indicate that the corresponding store location has already exist in table "item", plan 1.
						PreparedStatement pstmt1_1 = con.prepareStatement("UPDATE item SET amount=amount+? WHERE name=? AND brand=? AND unit=? AND store=?");
						PreparedStatement pstmt1_2 = con.prepareStatement("INSERT INTO item_history VALUES(null,?,?,?,?,?,?,?)");
						PreparedStatement pstmt1_3 = con.prepareStatement("UPDATE item SET amount=amount-? WHERE iid=?");
						
						pstmt1_1.setDouble(1, amount);
						pstmt1_1.setString(2, rsIID.getString("name"));
						pstmt1_1.setString(3, rsIID.getString("brand"));
						pstmt1_1.setString(4, rsIID.getString("unit"));
						pstmt1_1.setString(5, store);
						
						pstmt1_3.setDouble(1, amount);
						pstmt1_3.setLong(2, iid);
						
						pstmt1_2.setLong(1, rsItem.getLong("iid"));
						pstmt1_2.setDouble(2, -amount);
						pstmt1_2.setString(3, rsIID.getString("store"));
						pstmt1_2.setString(4, store);
						pstmt1_2.setTimestamp(5, new Timestamp(new java.util.Date().getTime()));
						pstmt1_2.setString(6, person);
						pstmt1_2.setString(7, description);
						
						pstmt1_1.execute();
						pstmt1_3.execute();
						pstmt1_2.execute();
					}
					else {
						// indicate that the corresponding store location does not exist in table "item", plan 2.
						PreparedStatement pstmt2_1 = con.prepareStatement("INSERT INTO item VALUES(null,?,?,?,?,?,?,?)",PreparedStatement.RETURN_GENERATED_KEYS);
						PreparedStatement pstmt2_2 = con.prepareStatement("INSERT INTO item_history VALUES(null,?,?,?,?,?,?,?)");
						PreparedStatement pstmt2_3 = con.prepareStatement("UPDATE item SET amount=amount-? WHERE iid=?");
						
						pstmt2_1.setString(1, rsIID.getString("name"));
						pstmt2_1.setString(2, rsIID.getString("brand"));
						pstmt2_1.setDouble(3, amount);
						pstmt2_1.setString(4, rsIID.getString("unit"));
						pstmt2_1.setString(5, store);
						pstmt2_1.setString(6, rsIID.getString("description"));
						pstmt2_1.setString(7, person);
						
						long iidTemp = 0;
						pstmt2_1.executeUpdate();
						ResultSet rsIK = pstmt2_1.getGeneratedKeys();	// Identity key
						if( rsIK.next() )
							iidTemp = rsIK.getLong(1);
						else
							iidTemp = Long.MAX_VALUE;
						rsIK.close();
						
						pstmt2_3.setDouble(1, amount);
						pstmt2_3.setLong(2, iid);
						
						pstmt2_2.setLong(1, iidTemp);
						pstmt2_2.setDouble(2, -amount);
						pstmt2_2.setString(3, rsIID.getString("store"));
						pstmt2_2.setString(4, store);
						pstmt2_2.setTimestamp(5, new Timestamp(new java.util.Date().getTime()));
						pstmt2_2.setString(6, person);
						pstmt2_2.setString(7, description);
						
						pstmt2_3.execute();
						pstmt2_2.execute();
					}
					rsItem.close();
				}
			}
			rsIID.close();
			//close work
			con.close();
			return true;
		} catch (SQLException e2) {
			e2.printStackTrace();
			return false;
		}
	}
	
}
